<?php
App::uses('AdminbaseController', 'Controller');
App::uses('Bhu', 'Lib');
class AdminController extends AdminbaseController {

    public $layout = 'admin_main'; 
    public $components = array('Paginator');   

    function beforeFilter() {
        parent::beforeFilter();
    }
	
	//后台首页

    function index(){   
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        $cc = $this->request->query('cc');
        $this->loadModel('Demo');                                  //加载数据表news
        $this->loadModel('Class');                               //加载数据表newsclass
        if(!empty($cc)){
        $cf = $cc;
        $class[] = $this->Class->findById($cc);
        $demo = $this->Demo->find('all',array('conditions' =>array('class'=> $cc),'order'=>array('id' =>'asc')));
        }else{
        $cf = 0;
        $class = $this->Class->find('all',array());
             $this->paginate = array(                                        //利用分页取出数据
                'conditions' =>array('class !=' => 1000 ),
                'order'=>array('id' =>'asc'),
                'limit'=>10
            );
            try{
                $demo = $this->paginate('Demo');
            }catch(NotFoundException $e){
                $demo = array();
            }   
        }
        foreach ($demo as $key => $value) { 
        if(intval($value['Demo']['class'])>0){ 
            $info = $this->Class->findById(intval($value['Demo']['class']));
            if(isset($info['Class']['id'])){   
                $demo[$key]['Demo']['cname']=$info['Class']['name'];
                }else{
                    $demo[$key]['Demo']['cname'] = "分类未选";
                }
            }else{
                $demo[$key]['Demo']['cname'] ="分类未选";
            }
        } 
        if(!empty($action) && !empty($id)){
            if(!$this->Demo->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
            $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/index');                               //跳回添加页面 
            }

        }
        $this->set('cf',$cf);
        $this->set('class',$class);
        $this->set('demo',$demo);                                 //把数据显示在视图页面上   
    }
    //新增分类

    function demo_class_add(){   
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');
        $cid = $this->request->query('cid');
        $this->loadModel('Class');                             //加载数据表column
            if($this->request->is('post')){                         //如果成功获取请求参数
                if(empty($params['name'])){
                    $this->ajaxReturn(4001, '请填写分类名称');      //判断数据是否输入    
                }
                $class = $this ->Class->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'name' => $params['name']
                        )
                    )
                );
                if(!empty($class)){                              //如果id为空，或者$New_class有的话
                    $this->ajaxReturn(4001, '分类名已存在'); 
                }
                if(!empty($cid) and empty($id)){
                    $class=array();
                    $class['Class']['cid'] = $cid;                               //写入数值
                    $class['Class']['name'] = $params['name'];
                    if(!$this->Class->save($class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/demo_class_manage');
                    }
                }
                if(!empty($id)){
                    $class=array();
                    $class['Class']['id'] = $id;                               //写入数值
                    $class['Class']['name'] = $params['name'];
                    if(!$this->Class->save($class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/demo_class_manage');
                    }
                }else{
                    $class=array();  
                    $class['Class']['name'] = $params['name'];                 //写入数值
                    if(!$this->Class->save($class)){                          //存储数据，如果失败则报错
                        $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                    }else{
                        $this->ajaxReturn(200, '操作成功！','','/admin/demo_class_manage');
                    }
                }
            }
        if(!empty($cid)){
            $cff = $this ->Class->find('first',array(            //通过id寻找数据表里是否有相同的id
                    'conditions' => array(
                        'id' => $cid
                        )
                    )
                );
            if(empty($cff)){
                $cf='木有';
            }else{
                $cf=$cff['Class']['name'];
            }
            $this->set('cf',$cf);
        }else{
            $cf='';
            $this->set('cf',$cf);
        } 
        if(!empty($id)){                                         //如果ID存在
            $class = $this->Class->findById($id);                  //通过ID找到数据
            $this->set('class',$class);                            //加载数据显示到视图层
        }
        $this->set('cid',$cid);  
    }

    //分类管理

    function demo_class_manage(){   
        $this->loadModel('Class');                                    //加载数据表NewsClass
        $id = $this->request->query('id');
        $action = $this->request->query('action');
        if(!empty($action) && !empty($id)){
            if(!$this->Class->delete(array('id'=>$id))){                        //存储数据，如果失败则报错
                $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
            }else{
                $this->ajaxReturn(300, '操作成功！');
                $this->render('/admin/demo_class_manage');                               //跳回添加页面 
            } 
        }
        $this->paginate = array(                                             //利用分页取出数据
            'conditions' =>array('cid' => 0 ), 
            'order'=>array('id' =>'asc'),
            'limit'=>10,
        );
        try{
            $big_class = $this->paginate('Class');
        }catch(NotFoundException $e){
            $big_class = array();
        }
        $small_class = $this->Class->find('all',array('conditions' =>array('cid !=' => 0 )));
        $this->set('big_class',$big_class);
        $this->set('small_class',$small_class);
    }

    //新增Demo

    function demo_add(){ 
        $params = $this->request->data;                             //获取post数据
        $id = $this->request->query('id');  
        $this->loadModel('Demo');                                 
        $this->loadModel('Class');
        $this->loadModel('AdminUser');
        $admin_user = $this->getUser();  
        if($this->request->is('post')){                         //如果成功获取请求参数
            if(empty($params['name'])){
                    $this->ajaxReturn(4001, '请填写名称');      //判断数据是否输入    
                }
            if(empty($params['class'])){
                    $this->ajaxReturn(4001, '请选择分类');      //判断数据是否输入    
                }
            if(empty($id)){
                $demo=array();
                $demo['Demo']['name'] = $params['name'];
                $demo['Demo']['class'] = $params['class'];
                $demo['Demo']['c_time'] = time();
                $demo['Demo']['anthor'] =  $admin_user['AdminUser']['c_name'];
                if(!$this->Demo->save($demo)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/index');
                }
            }else{
                $demo=array();
                $demo['Demo']['id'] = $id;                               //写入数值
                $demo['Demo']['name'] = $params['name'];
                $demo['Demo']['class'] = $params['class'];
                $demo['Demo']['c_time'] = time();
                $demo['Demo']['anthor'] = $admin_user['AdminUser']['c_name'];
                if(!$this->Demo->save($demo)){                          //存储数据，如果失败则报错
                    $this->ajaxReturn(4004, '操作失败，请稍后再试'); 
                }else{
                    $this->ajaxReturn(200, '操作成功！','','/admin/index');
                }
            }
        }
        $class = $this->Class->find('all',array());
        $webp=[];
        foreach ($class as $key => $value) {
            $webpa=$this->Class->findById($value['Class']['cid']);
            if(!empty($webpa) or $value['Class']['cid']==0 ){
                $webp[]=$value;
            }
        }
        if(!empty($id)){
            $demo=$this->Demo->findById($id);
            $nclass=[];
            $nclass['id']=$demo['Demo']['class'];
            $tclass=$this->Class->find('first',array('conditions' => array('id'=>$nclass['id'])));
            if(!empty($tclass)){
                $nclass['name']=$tclass['Class']['name'];
            }else{
                $nclass['name']="";
                $nclass['id']=1000;
            }
            $this->set('demo',$demo);
        }else{
            $nclass=[];
            $nclass['id']=0;
            $nclass['name']="选择分类";
            $checkbox=0;
        }
        $this->set('nclass',$nclass);
        $this->set('class',$webp);
    }
}